home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gxx / gpincl20.zoo / streambu.h < prev    next >
C/C++ Source or Header  |  1993-07-13  |  19KB  |  585 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15.  
  16.  
  17. #ifndef _STREAMBUF_H
  18. #define _STREAMBUF_H
  19. #ifdef __GNUG__
  20. #pragma interface
  21. #endif
  22.  
  23. /* #define _G_IO_THROW */ /* Not implemented:  ios::failure */
  24.  
  25. #include <g_config.h>
  26. #ifdef _G_NEED_STDARG_H
  27. #include <stdarg.h>
  28. #endif
  29.  
  30. #ifndef atarist
  31. #ifndef EOF
  32. #define EOF (-1)
  33. #endif
  34. #ifndef NULL
  35. #ifdef __GNUC__
  36. #define NULL ((void*)0)
  37. #else
  38. #define NULL (0)
  39. #endif
  40. #endif
  41. #else
  42. #include <stddef.h>
  43. #include <stdio.h>
  44. extern "C" unsigned long __DEFAULT_BUFSIZ__;
  45. #endif
  46.  
  47. class ostream; class streambuf; class backupbuf;
  48.  
  49. // In case some header files defines these as macros.
  50. #undef open
  51. #undef close
  52.  
  53. #ifdef _G_FRIEND_BUG
  54. extern int __UNDERFLOW(streambuf*);
  55. extern int __OVERFLOW(streambuf*, int);
  56. #endif
  57. extern "C" int __underflow(streambuf*);
  58. extern "C" int __overflow(streambuf*, int);
  59.  
  60. typedef _G_off_t streamoff;
  61. typedef _G_off_t streampos; // Should perhaps be _G_fpos_t ?
  62.  
  63. typedef unsigned long __fmtflags;
  64. typedef unsigned char __iostate;
  65.  
  66. struct _ios_fields { // The data members of an ios.
  67.     streambuf *_strbuf;
  68.     ostream* _tie;
  69.     int _width;
  70.     __fmtflags _flags;
  71.     _G_wchar_t _fill;
  72.     __iostate _state;
  73.     __iostate _exceptions;
  74.     int _precision;
  75. };
  76.  
  77. #define _IOS_GOOD    0
  78. #define _IOS_EOF    1
  79. #define _IOS_FAIL    2
  80. #define _IOS_BAD    4
  81.  
  82. #define _IOS_INPUT    1
  83. #define _IOS_OUTPUT    2
  84. #define _IOS_ATEND    4
  85. #define _IOS_APPEND    8
  86. #define _IOS_TRUNC    16
  87. #define _IOS_NOCREATE    32
  88. #define _IOS_NOREPLACE    64
  89. #define _IOS_BIN    128
  90. #ifdef atarist
  91.   /* force text mode */
  92. #define _IOS_TEXT    256
  93. #endif
  94.  
  95. #ifdef _STREAM_COMPAT
  96. enum state_value {
  97.     _good = _IOS_GOOD,
  98.     _eof = _IOS_EOF,
  99.     _fail = _IOS_FAIL,
  100.     _bad = _IOS_BAD };
  101. enum open_mode {
  102.     input = _IOS_INPUT,
  103.     output = _IOS_OUTPUT,
  104.     atend = _IOS_ATEND,
  105.     append = _IOS_APPEND
  106. #ifdef atarist
  107.     ,
  108.     in_bin =_IOS_INPUT|_IOS_BIN,  // force bin mode regardless of default mode
  109.     out_bin=_IOS_OUTPUT|_IOS_BIN,
  110.     app_bin=_IOS_APPEND|_IOS_BIN,
  111.     binary = _IOS_BIN,              // flag that may be or'ed in
  112.  
  113.     in_text=_IOS_INPUT|_IOS_TEXT,    // force text mode
  114.     out_text=_IOS_OUTPUT|_IOS_TEXT,
  115.     app_text=_IOS_APPEND|_IOS_TEXT,
  116.     text=_IOS_TEXT                // flag that may be or'ed in
  117. #endif
  118.  };
  119. #endif
  120.  
  121. class ios : public _ios_fields {
  122.   public:
  123.     typedef __fmtflags fmtflags;
  124.     typedef int iostate;
  125.     typedef int openmode;
  126.     typedef int streamsize;
  127.     enum io_state {
  128.     goodbit = _IOS_GOOD,
  129.     eofbit = _IOS_EOF,
  130.     failbit = _IOS_FAIL,
  131.     badbit = _IOS_BAD };
  132.     enum open_mode {
  133.     in = _IOS_INPUT,
  134.     out = _IOS_OUTPUT,
  135.     ate = _IOS_ATEND,
  136.     app = _IOS_APPEND,
  137.     trunc = _IOS_TRUNC,
  138.     nocreate = _IOS_NOCREATE,
  139.     noreplace = _IOS_NOREPLACE,
  140.     bin = _IOS_BIN
  141. #ifdef atarist
  142.     ,
  143.     in_bin = _IOS_INPUT|_IOS_BIN,
  144.     out_bin = _IOS_OUTPUT|_IOS_BIN,
  145.         ate_bin = _IOS_ATEND|_IOS_BIN,
  146.     app_bin = _IOS_APPEND|_IOS_BIN,
  147.         binary = _IOS_BIN,
  148.  
  149.     in_text = _IOS_INPUT|_IOS_TEXT,
  150.     out_text = _IOS_OUTPUT|_IOS_TEXT,
  151.         ate_text = _IOS_ATEND |_IOS_TEXT,
  152.     app_text = _IOS_APPEND|_IOS_TEXT,
  153.         text = _IOS_TEXT
  154. #endif
  155.  };
  156.     enum seek_dir { beg, cur, end};
  157.     // ANSI: typedef enum seek_dir seekdir; etc
  158.     enum { skipws=01, left=02, right=04, internal=010,
  159.        dec=020, oct=040, hex=0100,
  160.        showbase=0200, showpoint=0400, uppercase=01000, showpos=02000,
  161.        scientific=04000, fixed=010000, unitbuf=020000, stdio=040000,
  162.        dont_close=0100000 //Don't delete streambuf on stream destruction
  163.        };
  164.     enum { // Masks.
  165.     basefield=dec+oct+hex,
  166.     floatfield = scientific+fixed,
  167.     adjustfield = left+right+internal
  168.     };
  169.  
  170. #ifdef _G_IO_THROW
  171.     class failure : public xmsg {
  172.     ios* _stream;
  173.       public:
  174.     failure(ios* stream) { _stream = stream; }
  175.     failure(string cause, ios* stream) { _stream = stream; }
  176.     ios* rdios() const { return _stream; }
  177.     };
  178. #endif
  179.  
  180.     ostream* tie() const { return _tie; }
  181.     ostream* tie(ostream* val) { ostream* save=_tie; _tie=val; return save; }
  182.  
  183.     // Methods to change the format state.
  184.     _G_wchar_t fill() const { return (_G_wchar_t)_fill; }
  185.     _G_wchar_t fill(_G_wchar_t newf)
  186.     {_G_wchar_t oldf = (_G_wchar_t)_fill; _fill = (char)newf; return oldf;}
  187.     fmtflags flags() const { return _flags; }
  188.     fmtflags flags(fmtflags new_val) {
  189.     fmtflags old_val = _flags; _flags = new_val; return old_val; }
  190.     int precision() const { return _precision; }
  191.     int precision(int newp) {
  192.     unsigned short oldp = _precision; _precision = (unsigned short)newp;
  193.     return oldp; }
  194.     fmtflags setf(fmtflags val) {
  195.     fmtflags oldbits = _flags;
  196.     _flags |= val; return oldbits; }
  197.     fmtflags setf(fmtflags val, fmtflags mask) {
  198.     fmtflags oldbits = _flags;
  199.     _flags = (_flags & ~mask) | (val & mask); return oldbits; }
  200.     fmtflags unsetf(fmtflags mask) {
  201.     fmtflags oldbits = _flags & mask;
  202.     _flags &= ~mask; return oldbits; }
  203.     int width() const { return _width; }
  204.     int width(int val) { int save = _width; _width = val; return save; }
  205.  
  206. #ifdef _G_IO_THROW
  207.     void _throw_failure() { throw new ios::failure(this); }
  208. #else
  209.     void _throw_failure() { }
  210. #endif
  211.  
  212.     streambuf* rdbuf() const { return _strbuf; }
  213.     void clear(iostate state = 0) {
  214.     _state = _strbuf ? state : state|badbit;
  215.     if (_state & _exceptions) _throw_failure(); }
  216.     void set(iostate flag) { _state |= flag;
  217.     if (_state & _exceptions) _throw_failure(); }
  218.     void setstate(iostate flag) { _state |= flag; // ANSI
  219.     if (_state & _exceptions) _throw_failure(); }
  220.     int good() const { return _state == 0; }
  221.     int eof() const { return _state & ios::eofbit; }
  222.     int fail() const { return _state & (ios::badbit|ios::failbit); }
  223.     int bad() const { return _state & ios::badbit; }
  224.     iostate rdstate() const { return _state; }
  225.     operator void*() const { return fail() ? (void*)0 : (void*)(-1); }
  226.     int operator!() const { return fail(); }
  227.     iostate exceptions() const { return _exceptions; }
  228.     void exceptions(iostate enable) {
  229.     _exceptions = enable;
  230.     if (_state & _exceptions) _throw_failure(); }
  231.  
  232.     static int sync_with_stdio(int on);
  233.     static void sync_with_stdio() { sync_with_stdio(1); }
  234.  
  235. #ifdef _STREAM_COMPAT
  236.     void unset(state_value flag) { _state &= ~flag; }
  237.     void close();
  238.     int is_open();
  239.     int readable();
  240.     int writable();
  241. #endif
  242.  
  243.     // Used to initialize standard streams. Not needed in this implementation.
  244.     class Init {
  245.     public:
  246.       Init () { }
  247.     };
  248.  
  249.   protected:
  250.     ios(streambuf* sb = 0, ostream* tie = 0);
  251.     virtual ~ios();
  252.     void init(streambuf* sb) { _state=0; _strbuf=sb; }
  253. };
  254.  
  255. #if __GNUG__==1
  256. typedef int _seek_dir;
  257. #else
  258. typedef ios::seek_dir _seek_dir;
  259. #endif
  260.  
  261. // Magic numbers and bits for the _flags field.
  262. // The magic numbers use the high-order bits of _flags;
  263. // the remaining bits are abailable for variable flags.
  264. // Note: The magic numbers must all be negative if stdio
  265. // emulation is desired.
  266.  
  267. #define _IO_MAGIC 0xFBAD0000 /* Magic number */
  268. #define _OLD_STDIO_MAGIC 0xFABC0000 /* Emulate old stdio. */
  269. #define _IO_MAGIC_MASK 0xFFFF0000
  270. #define _S_USER_BUF 1 /* User owns buffer; don't delete it on close. */
  271. #define _S_UNBUFFERED 2
  272. #define _S_NO_READS 4 /* Reading not allowed */
  273. #define _S_NO_WRITES 8 /* Writing not allowd */
  274. #define _S_EOF_SEEN 0x10
  275. #define _S_ERR_SEEN 0x20
  276. #define _S_DELETE_DONT_CLOSE 0x40
  277. #define _S_LINKED 0x80 // Set if linked (using _chain) to streambuf::_list_all.
  278. #define _S_IN_BACKUP 0x100
  279. #define _S_LINE_BUF 0x200
  280. #define _S_TIED_PUT_GET 0x400 // Set if put and get pointer logicly tied.
  281. #define _S_CURRENTLY_PUTTING 0x800
  282. #define _S_IS_APPENDING 0x1000
  283. #define _S_IS_BACKUPBUF 0x